<?php

namespace app\store\model;
use app\common\model\ProjectTasks as ProjectTasksModel;
use app\store\model\Material;
class ProjectTasks extends ProjectTasksModel
{
    /**
     * 获取列表
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($param)
    {
        $where = [];
        if(!empty($param['company_id'])){
            $where['pt.company_id'] = $param['company_id'];
        }
        if(!empty($param['user_name'])){
            $where['u.user_name'] = ['like',"%{$param['user_name']}%"];
        }
        $data = $this->alias('pt')->field('pt.*,u.user_name')->join('user_info u','pt.user_id=u.id','left')->where($where)->order(['pt.create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => request()->request()
            ]);
        if(!empty($data)){
            foreach ($data as $item){
                if($item->entry_type){
                    $item->entry_type_name = entryType($item->entry_type);
                }
            }
        }
        return $data;
    }

    /**
     * 新增记录
     * @param $data
     * @return false|int
     */
    public function add($data){
        return $this->allowField(true)->save($data);
    }

    /**
     * 更新记录
     * @param $data
     * @param $id
     * @return false|int
     */
    public function edit($data,$id){
        return $this->allowField(true)->save($data,$id);
    }

    /**
     * 删除
     * @param $id
     * @return int
     */
    public static function del($id){
        return self::destroy($id);
    }

    /**
     * 获取列表
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getExamineList($company_id,$entry_name)
    {
        $where['pt.company_id'] = $company_id;
        $where['pt.entry_status'] = self::REVIEWED;
        if(!empty($entry_name)){
            $where['pt.entry_name'] = ['like',"%{$entry_name}%"];
        }
        $data = $this->alias('pt')->field('pt.*,u.user_name')->join('user_info u','pt.user_id=u.id','left')->where($where)->order(['pt.create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => request()->request()
            ]);
        if(!empty($data)){
            foreach ($data as $item){
                if($item->entry_type){
                    $item->entry_type_name = entryType($item->entry_type);
                }
            }
        }
        return $data;
    }

    /**
     * 根据user_id  根据用户id统计任务维修类型
     * @param $user_id
     * @return mixed
     * @throws \think\db\exception\BindParamException
     * @throws \think\exception\PDOException
     */
    public function information($user_id){
        $sql = "select CASE entry_type  when 1000 then 'project_task_id' when 890 then 'repair_task_id' when 120 then 'fixed_dispatch_id' ELSE 'default' END AS entry_name, count(entry_type) as type_num from yoshop_project_tasks where user_id={$user_id} and entry_status=3 group by entry_type";
        $row = $this->query($sql);
        return $row;
    }
    /**
     * 统计公司的维修数据
     * @throws \think\db\exception\BindParamException
     * @throws \think\exception\PDOException
     */
    public function statistics(){
        $company = getCompany();
        foreach ($company as $key=> $item){
            $sql = "select CASE entry_type  when 1000 then '工程任务' when 890 then '维修任务' when 120 then '固定派单' ELSE 'default' END AS entry_name, count(entry_type) as type_num from yoshop_project_tasks where company_id={$item['id']} and entry_status=".self::DISPATCH." group by entry_type";
            $data = $this->query($sql);
            $company[$key]['list'] = $data??[];
            $company[$key]['total'] = $this->where(['company_id'=>$item['id'],'entry_status'=>self::DISPATCH])->count()??0;
           // echo $this->getLastSql();exit();
        }
        return $company;
    }

    /**
     * 获取统计订单列表
     * @param $param
     * @return \think\Paginator
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function orderStatistics($param){
        $day_start_time=strtotime(date("Y-m-d",time()));
        $day_end_time=$day_start_time+60*60*24;
        $startTime = strtotime(date('Y-m-01',time()));//获取该月份的第一天
        $endTime = strtotime(date('Y-m-t 23:59:59',time()));//获取该月份的最后一天
        $whereday['create_time'] = ['between',[$day_start_time,$day_end_time]];
        $whereyue['create_time'] = ['between',[$startTime,$endTime]];
        $whereday['entry_status'] =$whereyue['entry_status'] =$where['pt.entry_status'] = 3;
        if(!empty($param['entry_name'])){
            $whereday['entry_name']=$whereyue['entry_name'] =$where['pt.entry_name'] = ['like',"%{$param['entry_name']}%"];
        }
        if(!empty($param['company_id'])){
            $where['pt.company_id'] = $param['company_id'];
        }
        /*if(!empty($param['start_time']) && !empty($param['end_time'])){
            $stime = strtotime($param['start_time']);$etime = strtotime($param['end_time']);
            $where['pt.create_time'] = ['between',[$stime,$etime]];
        }*/
        if(!empty($param['entry_type'])){
            $whereday['entry_type'] = $whereyue['entry_type'] = $where['pt.entry_type'] = $param['entry_type'];
        }
         if(!empty($param['start_time'])){
            $start_time =  date('Y-m-d 00:00:00',strtotime($param['start_time']));
            $end_time = date('Y-m-d 23:59:59',strtotime($param['start_time']));
            //$this->whereBetween('pt.dispatch_time',$start_time,$end_time);
             $where['pt.dispatch_time'] = ['between',[$start_time,$end_time]];
        }
        $day_num = $this->where($whereday)->count('id');
        $yue_mum = $this->where($whereyue)->count('id');
        $data = $this->alias('pt')->field('pt.*,u.user_name')->join('user_info u','pt.user_id=u.id','left')->where($where)->order(['pt.create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => request()->request()
            ]);
        if(!empty($data)){
            foreach ($data as $item){
                if($item->entry_type){
                    $item->entry_type_name = entryType($item->entry_type);
                }
            }
        }
        return ['data'=>$data,'day_total'=>$day_num,'month_total'=>$yue_mum];
    }

    /**
     * 获取任务详情
     * @param $id
     * @return array|bool|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function detail($id){
        $detail = $this->find($id);
        if(!empty($detail)){
            $detail->material = Material::list($detail->id);
        }
        return $detail;
    }
}